<?php

namespace hotel\common\utils;

use hotel\common\models\HotelSession;
use hotel\common\models\XopConfig;
use hotel\common\utils\HttpClient;
use yii\log\FileTarget;

class HotelServices
{
    private static $hotel_url = 'https://klr-rsp.xms.foxhis.com/xmsopen-web/rest';
	//http://36.155.114.115:8180/xmsopen-web/rest
    //private static $hotel_url = 'http://xop360.test.foxhis.com/xmsopen-web/rest';
    private static $ver = '1.0.0';
    private static $loc = 'zh_CN';


    //平台登录
    public static function login($appid)
    {
        $xop_config_data = XopConfig::find()->where("appid = '$appid'")->asArray()->one();
        $ts = self::msectime();
        $data = [
            'appkey' => $xop_config_data['appkey'],
            'secret' => $xop_config_data['secret'],
            'method' => 'xmsopen.public.login',
            'ver' => self::$ver,
            'hotelid' => $xop_config_data['jituan_hotelid'],
            'loc' => self::$loc,
            'ts' => $ts
        ];
        $data['sign'] = self::createSign([], $ts, $xop_config_data);
        $result = HttpClient::post(self::$hotel_url, json_encode($data));
        $res = json_decode($result, true);
        $expire_time = date('Y-m-d H:i:s', time() + 3600 * 24 * 7);
        $hotel_session_data = HotelSession::find()->where("appid = '$appid'")->asArray()->one();
        $id = $hotel_session_data['id'];
        $model = HotelSession::findone($id);
        $model->session = $res['session'];
        $model->expire_time = $expire_time;
        $model->save();
        return $res;
    }

//登出
    public static function loginOut($session, $appid)
    {
        $xop_config_data = XopConfig::find()->where("appid = '$appid'")->asArray()->one();
        $ts = self::msectime();
        $data = [
            'appkey' => $xop_config_data['appkey'],
            'session' => $session,
            'method' => 'xmsopen.public.logout',
            'ver' => self::$ver,
            'hotelid' => $xop_config_data['jituan_hotelid'],
            'loc' => self::$loc,
            'ts' => $ts
        ];
        $data['sign'] = self::createSign([], $ts, $xop_config_data);
        $result = HttpClient::post(self::$hotel_url, json_encode($data));
        return json_decode($result, true);
    }

    //
    public static function xopgetmasterguestinfo($session, $ident, $appid)
    {
        $xop_config_data = XopConfig::find()->where("appid = '$appid'")->asArray()->one();
        $ts = self::msectime();
        $data = [
            'appkey' => self::$appkey,
            'session' => $session,
            'method' => 'xmsopen.reservation.xopgetmasterguestinfo',
            'ver' => self::$ver,
            'hotelid' => self::$jituan_hotelid,
            'loc' => self::$loc,
            'ts' => $ts,
            'params' => [
                [
                    'hotelid' => self::$yewu_hotelid,
                    'ident' => $ident,
                    'sta' => 'I,O,H,S,R'
                ]
            ]
        ];
        $params = [];
        $params['ident'] = $ident;
        $params['hotelid'] = self::$yewu_hotelid;
        $params['sta'] = 'I,O';
        $data['sign'] = self::createSign($params, $ts, $xop_config_data);
        $result = HttpClient::post(self::$hotel_url, json_encode($data));
        return json_decode($result, true);
    }


    //查询酒店
    public static function xopgethotelinfo($session, $appid)
    {
        $xop_config_data = XopConfig::find()->where("appid = '$appid'")->asArray()->one();
        $ts = self::msectime();
        $data = [
            'appkey' => $xop_config_data['appkey'],
            'session' => $session,
            'method' => 'xmsopen.common.xopgethotelinfo',
            'ver' => self::$ver,
            'hotelid' => $xop_config_data['jituan_hotelid'],
            'loc' => self::$loc,
            'ts' => $ts,
            'params' => [
                [
                    'hotelid' => $xop_config_data['yewu_hotelid']
                ]
            ]
        ];
        $params = [];
        $params['hotelid'] = $xop_config_data['yewu_hotelid'];
        $data['sign'] = self::createSign($params, $ts, $xop_config_data);
        $result = HttpClient::post(self::$hotel_url, json_encode($data));
        return json_decode($result, true);
    }

    //获取房型
    public static function xopgetroomtype($session, $appid)
    {
        $xop_config_data = XopConfig::find()->where("appid = '$appid'")->asArray()->one();
        $ts = self::msectime();
        $data = [
            'appkey' => $xop_config_data['appkey'],
            'session' => $session,
            'method' => 'xmsopen.reservation.xopgetroomtype',
            'ver' => self::$ver,
            'hotelid' => $xop_config_data['jituan_hotelid'],
            'loc' => self::$loc,
            'ts' => $ts,
            'cmmcode' => $xop_config_data['cmmcod'],
            'params' => [
                [
                    'hotelid' => $xop_config_data['yewu_hotelid'],
                ]
            ]
        ];
        $params = [];
        $params['hotelid'] = $xop_config_data['yewu_hotelid'];
        $data['sign'] = self::createSign($params, $ts, $xop_config_data);
        $result = HttpClient::post(self::$hotel_url, json_encode($data));
        return json_decode($result, true);
    }


    //房价查询
    public static function xopratequery($session, $appid, $arr, $dep)
    {
        $xop_config_data = XopConfig::find()->where("appid = '$appid'")->asArray()->one();
        $ts = self::msectime();
        $data = [
            'appkey' => $xop_config_data['appkey'],
            'session' => $session,
            'method' => 'xmsopen.reservation.xopratequery',
            'ver' => self::$ver,
            'hotelid' => $xop_config_data['jituan_hotelid'],
            'loc' => self::$loc,
            'ts' => $ts,
            'cmmcode' => $xop_config_data['cmmcod'],
            'params' => [
                [
                    'hotelid' => $xop_config_data['yewu_hotelid'],
                    'arr' => $arr,
                    'dep' => $dep,
                    'getdailyrate' => 'T',
                    'getall' => 'T'
                ]
            ]
        ];
        $params = [];
        $params['hotelid'] = $xop_config_data['yewu_hotelid'];
        $params['arr'] = $arr;
        $params['dep'] = $dep;
        $data['sign'] = self::createSign($params, $ts, $xop_config_data);
        $result = HttpClient::post(self::$hotel_url, json_encode($data));
        return json_decode($result, true);
    }

    //保存订单
    public static function xopsavereservation($session, $appid, $post_data,$dailyrate)
    {
        $xop_config_data = XopConfig::find()->where("appid = '$appid'")->asArray()->one();
        $ts = self::msectime();
        $data = [
            'appkey' => $xop_config_data['appkey'],
            'session' => $session,
            'method' => 'xmsopen.reservation.xopsavereservation',
            'ver' => self::$ver,
            'hotelid' => $xop_config_data['jituan_hotelid'],
            'loc' => self::$loc,
            'ts' => $ts,
            'cmmcode' => $xop_config_data['cmmcod'],
            'params' => [
                [
                    'ratecode' => $post_data['ratecode'],
                    'rmtype' => $post_data['rmtype'],
                    'rmnum' => $post_data['rmnum'],
                    'rate' => $post_data['rate'],
                    'contact_mobile' => $post_data['phone'],
                    'hotelid' => $xop_config_data['yewu_hotelid'],
                    'name' => $post_data['name'],
                    'dep' => substr($post_data['dep'], 0, 10),
                    'arr' => substr($post_data['arr'], 0, 10),
                    'gstno' => 1,
                    'restype' => $xop_config_data['restype'],
                    'mobile' => $post_data['phone'],
                    'ref' => '订单 '.$post_data['couponname'],
                    'dailyrate' =>$dailyrate
                ]
            ]
        ];
        $params = [];
        $params['ratecode'] = $post_data['ratecode'];
        $params['rmtype'] = $post_data['rmtype'];
        $params['rmnum'] = $post_data['rmnum'];
        $params['rate'] = $post_data['rate'];
        $params['rmrate'] = round($post_data['pay_price'] / $post_data['rmnum'], 2);
        $params['hotelid'] = $xop_config_data['yewu_hotelid'];
        $params['name'] = $post_data['name'];
        $params['dep'] = substr($post_data['dep'], 0, 10);
        $params['arr'] = substr($post_data['arr'], 0, 10);
        $params['gstno'] = $post_data['gstno'];
        $params['restype'] = $xop_config_data['restype'];
        $data['sign'] = self::createSign($params, $ts, $xop_config_data);
        $result = HttpClient::post(self::$hotel_url, json_encode($data));
        return json_decode($result, true);
    }


    //查询订单
    public static function xopqueryreservation($session, $appid, $post_data)
    {
        $xop_config_data = XopConfig::find()->where("appid = '$appid'")->asArray()->one();
        $ts = self::msectime();
        $data = [
            'appkey' => $xop_config_data['appkey'],
            'session' => $session,
            'method' => 'xmsopen.reservation.xopqueryreservation',
            'ver' => self::$ver,
            'hotelid' => $xop_config_data['jituan_hotelid'],
            'loc' => self::$loc,
            'ts' => $ts,
            'params' => [
                [
                    'hotelid' => $xop_config_data['yewu_hotelid'],
                    'accnt' => $post_data['accnt']
                ]
            ]
        ];
        $params = [];
        $params['hotelid'] = $xop_config_data['yewu_hotelid'];
        $params['accnt'] = $post_data['accnt'];
        $data['sign'] = self::createSign($params, $ts, $xop_config_data);
        $result = HttpClient::post(self::$hotel_url, json_encode($data));
        return json_decode($result, true);
    }

    //网站预付入账
    public static function xopdopaymentaccount($session, $appid, $order)
    {
        $xop_config_data = XopConfig::find()->where("appid = '$appid'")->asArray()->one();
        $ts = self::msectime();
        $data = [
            'appkey' => $xop_config_data['appkey'],
            'session' => $session,
            'method' => 'xmsopen.accounts.xopdopaymentaccount',
            'ver' => self::$ver,
            'hotelid' => $xop_config_data['jituan_hotelid'],
            'loc' => self::$loc,
            'ts' => $ts,
            'cmmcode' => $xop_config_data['cmmcod'],
            'params' => [
                [
                    'hotelid' => $xop_config_data['yewu_hotelid'],
                    'pay_money' => $order['pay_price'],
                    'pay_code' => $xop_config_data['pay_code'],
                    'payno' => $order['order_sn'],
                    'canceluse' => 'T',
                    'remark' => '订房',
                    'accnt' => $order['accnt']
                ]
            ]
        ];
        $params = [];
        $params['pay_money'] = $order['pay_money'];
        $params['pay_code'] = $xop_config_data['pay_code'];
        $params['payno'] = $order['order_sn'];
        $params['hotelid'] = $xop_config_data['yewu_hotelid'];
        $params['remark'] = '订房';
        $params['canceluse'] = 'T';
        $params['accnt'] = $order['accnt'];
        $data['sign'] = self::createSign($params, $ts, $xop_config_data);
        $result = HttpClient::post(self::$hotel_url, json_encode($data));
        return json_decode($result, true);
    }

    //网站预付入账冲销
    public static function xopwriteoffpayaccount($session, $appid, $order)
    {
        $xop_config_data = XopConfig::find()->where("appid = '$appid'")->asArray()->one();
        $ts = self::msectime();
        $data = [
            'appkey' => $xop_config_data['appkey'],
            'session' => $session,
            'method' => 'xmsopen.accounts.xopwriteoffpayaccount',
            'ver' => self::$ver,
            'hotelid' => $xop_config_data['jituan_hotelid'],
            'loc' => self::$loc,
            'ts' => $ts,
            'params' => [
                [
                    'hotelid' => $xop_config_data['yewu_hotelid'],
                    'payno' => $order['order_sn'],
                    'accnt' => $order['accnt'],
                    'empno' => $xop_config_data['empno']
                ]
            ]
        ];
        $params = [];
        $params['hotelid'] = $xop_config_data['yewu_hotelid'];
        $params['payno'] = $order['order_sn'];
        $params['accnt'] = $order['accnt'];
        $params['empno'] = $xop_config_data['empno'];
        $data['sign'] = self::createSign($params, $ts, $xop_config_data);
        $result = HttpClient::post(self::$hotel_url, json_encode($data));
        return json_decode($result, true);
    }

    //取消订单
    public static function xopcxlreservation($session, $appid, $order)
    {
        $xop_config_data = XopConfig::find()->where("appid = '$appid'")->asArray()->one();
        $ts = self::msectime();
        $data = [
            'appkey' => $xop_config_data['appkey'],
            'session' => $session,
            'method' => 'xmsopen.reservation.xopcxlreservation',
            'ver' => self::$ver,
            'hotelid' => $xop_config_data['jituan_hotelid'],
            'loc' => self::$loc,
            'ts' => $ts,
            'cmmcode' => $xop_config_data['cmmcod'],
            'params' => [
                [
                    'hotelid' => $xop_config_data['yewu_hotelid'],
                    'cxlreason' => '取消订单',
                    'force' => 'T',
                    'rsvno' => $order['rsvno'],
                ]
            ]
        ];
        $params = [];
        $params['hotelid'] = $xop_config_data['hotelid'];
        $params['cxlreason'] = '取消订单';
        $params['force'] = 'T';
        $params['rsvno'] = $order['rsvno'];
        $data['sign'] = self::createSign($params, $ts, $xop_config_data);
        $result = HttpClient::post(self::$hotel_url, json_encode($data));
        return json_decode($result, true);
    }


    //签名生成
    public static function createSign($params = [], $ts, $xop_config_data)
    {
        $signText = '';
        $signText .= $xop_config_data['appkey'] . $xop_config_data['jituan_hotelid'] . self::$loc . $ts;
        if (!empty($params)) {
            sort($params);
            foreach ($params as $k => $v) {
                if (!empty($v)) {
                    $signText .= strtoupper($v);
                }
            }
        }
        $signText .= $xop_config_data['secret'];
        return md5($signText);
    }


    //返回当前的毫秒时间戳
    public static function msectime()
    {
        list($msec, $sec) = explode(' ', microtime());
        $msectime = (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
        return $msectime;
    }


    //判断session是否过期
    public static function checkXopSession($appid)
    {
        $res = HotelSession::find()->where("appid = '$appid'")->asArray()->one();
        if (empty($res['session'])) {
            $session_data = HotelServices::login($appid);
            $session = $session_data['session'];
        } else {
            $expire_time = $res['expire_time'];
            $now_time = date('Y-m-d H:i:s');
            if ($expire_time < $now_time) {
                $session_data = HotelServices::login($appid);
                $session = $session_data['session'];
            } else {
                $session = $res['session'];
            }
        }
        return $session;
    }
}